/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_CMOS.h
 *
 *  Created on: 2023年2月20日
 *      Author: 64435
 */

#ifndef PERIPHERAL_LAYER_INC_LOS_CMOS_H_
#define PERIPHERAL_LAYER_INC_LOS_CMOS_H_

#include "LOS_SYS.h"

typedef struct LOS_CMOS_t
{
    /* 健康管理状态量 */
    uint8_t VAL;                 //数据有效性标志，0：有效， 其他值：无效
    uint8_t CMD;                 //开关机指令状态,发送开机指令后置1，发送关机指令后置0
    uint8_t PWR;                 //电源状态，读取电源遥测，通电为1，断电为0
    uint8_t MSG;                 //通信状态，通信正常为0，通信异常为其他值
    uint8_t SWNUM;
    uint8_t RBCNT;               //复位次数
    uint8_t NFR;                 //Need For Reset 需要复位标志位，1为需要复位，0为不需要
    uint8_t REC;                //CAN识别标志位,0:未识别  1：识别
    uint8_t HL_TXF;              //HL_前缀表示健康管理相关变量。遥测请求发送完成标志,发送一次后该量置1，接收到回应后置0(TX FINISHED)
    uint8_t HL_WCNT;             //等待接收计数器。该计数器在操作系统的一个线程中累加，当HL_TXF为1时该值+1，HL_TXF为0时该值清0，当累加到一定值时系统会判断通信异常，并将MSG置1

    /* 本机工参 */
    uint8_t bright;
    uint8_t contrast;
    uint8_t sharp;
    uint8_t ev;
    uint8_t compress;
    uint8_t color;
    uint32_t lastpicid;  //最近一张照片编号
    uint32_t lastpiclen; //最近一张照片长度

    /* 照片读取相关变量 */
    uint8_t picid_TC;    //通过遥控指令要读取的照片id 
    uint32_t picid;      //读到的照片id
    uint32_t offset;     //从CAN获取的偏移地址
    uint32_t size;       //数据块大小
    uint8_t picbuf[256]; //照片缓存
    uint8_t readsta;     //照片读取状态：0未开始 1获取照片信息 2读取中 3文件系统错误 4相机无响应读取超时 5读取结束
    uint32_t readoffset; //照片读取偏移地址，读到一包后+256
    uint8_t recnt;       //重复读取一个数据块的次数
    uint32_t filesize;   //cms.bin文件的大小
    uint32_t pictotalsize;  //cms.bin里所有照片大小的总和
    uint8_t piccnt;       //cms.bin中存储的照片总数量
    uint8_t picid_TM;     //要通过测控回传地面的照片id
    
}LOS_CMOS_t;

LOS_CMOS_t STA_CMOS;

extern uint8_t photo_buf[512];
extern uint8_t cmos_tmbuf[140];
extern uint8_t cmos_sdmsg[1024];
extern uint8_t cmos_p2gbuf[218];
extern uint8_t pbready;

void LOS_CMOS_Snap();
void LOS_CMOS_SendRecentPhoto();
void LOS_CMOS_SendRecentPhotoMsg();
void LOS_CMOS_STA();
void LOS_CMOS_JPEG();
void LOS_CMOS_FormatFlash();
void LOS_CMOS_AllPhotoMsg();
void LOS_CMOS_DefaultArg();
void LOS_CMOS_CheckArg();
void LOS_CAN_CMOSCallBack(LOS_CANMSG_t* can_re);
void LOS_CMOS_ReadPhoto(uint32_t photoid, uint32_t offset, uint32_t length);
void LOS_CMOS_PhotoLength(uint8_t photoid);
void LOS_CMOS_PhotoArg(uint8_t photoid);
void LOS_CMOS_SetArg(uint8_t bright, uint8_t contrast, uint8_t sharp, uint8_t ev, uint8_t compress,uint8_t color);
void LOS_CMOS_FileInit();
void LOS_CMOS_FileReInit();
uint8_t LOS_CMOS_GetSDPicNum(uint32_t* totalsize);
void LOS_CMOS_UpdateSDMSG(uint8_t picnum, uint32_t length);
int8_t LOS_CMOS_Pic2EarthPack(uint8_t picnum, uint8_t* pbuf);
void LOS_CMOS_PWROn();
void LOS_CMOS_PWROff();

#endif /* PERIPHERAL_LAYER_INC_LOS_CMOS_H_ */
